/*
 * Filename:    IndicationServiceImpl.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年10月17日
 */
package com.paic.mhis.hcpms.quota.settlement.biz.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.biz.service.KpiCoreDataService;
import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.dto.KpiCateDTO;
import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.dto.KpiConditionDTO;
import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.dto.KpiDeptDTO;
import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.dto.KpiDoctorDTO;
import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.dto.KpiGradeDTO;
import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.dto.KpiMedicalOrgDTO;
import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.dto.KpiSociationDTO;
import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.dto.PaginationDTO;
import com.paic.mhis.hcpms.quota.indicatorconfig.dto.IndicatorBaseDTO;
import com.paic.mhis.hcpms.quota.settlement.biz.service.IndicationService;
import com.paic.mhis.hcpms.quota.settlement.common.Constants;
import com.paic.mhis.hcpms.quota.settlement.dto.IndicationBeanDTO;
import com.paic.mhis.hcpms.quota.settlement.dto.IndicationDeptDTO;
import com.paic.mhis.hcpms.quota.settlement.dto.IndicationQueryParameterDTO;
import com.paic.mhis.hcpms.quota.settlement.dto.IndicationRegionOrgDTO;
import com.paic.mhis.hcpms.quota.settlement.dto.IndicationRegionOrgPageBean;
import com.paic.mhis.hcpms.quota.settlement.dto.IndicationTitleDTO;
import com.paic.mhis.hcpms.quota.settlement.dto.MedicalOrgDTO;
import com.paic.mhis.hcpms.quota.settlement.intergration.dao.ICodeLabelDAO;

@Component("indicationServiceImpl")
public class IndicationServiceImpl implements IndicationService {
	@Autowired
	private KpiCoreDataService kpiCoreDataService;
	@Autowired
	private ICodeLabelDAO iCodeLabelDAO;
	@Autowired
	private IndicationHospitalHelp indicationHospitalHelp; // 查询指标第一层医院级别
	@Autowired
	private IndicationRegionOrgHelp indicationRegionOrgHelp;// 查询第二层级别下的医院
	@Autowired
	private IndicationRegionDeptHelp indicationRegionDeptHelp;// 查询医院下的科室信息
	@Autowired
	private IndicationDoctorHelp indicationDoctorHelp; // 查询科室下面的医生

	/**
	 * 根据机构类型查询指标信息
	 * 
	 * @param condition
	 * @return
	 */
	public List<IndicationBeanDTO> queryIndicationByOrgCategory(IndicationQueryParameterDTO condition) {
		String indication = condition.getIndicator();// 指标
		String startTime = condition.getDate().getStart(); // 开始时间
		String endTime = condition.getDate().getEnd();// 结束时间
		String visitType = condition.getVisit();// 门诊类型
		String regionCode = condition.getRegionCode();
		String orgCategory = condition.getOrgCategory(); // 机构类型

		KpiConditionDTO kpiConditionDTO = new KpiConditionDTO();
		kpiConditionDTO.setVisitType(visitType);
		kpiConditionDTO.setKpiTimeStart(startTime);
		kpiConditionDTO.setKpiTimeEnd(endTime);
		kpiConditionDTO.setKpiCode(indication);
		kpiConditionDTO.setPeriod(Constants.perion_13); // 按照月份
		kpiConditionDTO.setSociationCode(regionCode);

		List<String> cateList = new ArrayList<String>();
		cateList.add(orgCategory);

		kpiConditionDTO.setCateList(cateList);

		List<KpiCateDTO> KpiCateDTOList = this.kpiCoreDataService.queryKpiByCate(kpiConditionDTO);

		List<IndicationBeanDTO> list = null;

		if (null != KpiCateDTOList && KpiCateDTOList.size() > 0) {
			list = this.indicationHospitalHelp.getIndicationBeanDTOByOrgCategory(KpiCateDTOList, kpiConditionDTO);
			Collections.sort(list);
		}

		return list;

	}

	/**
	 * 根据指标类型和医院等级查询
	 */
	@Override
	public List<IndicationBeanDTO> queryIndication(IndicationQueryParameterDTO condition) {

		String indication = condition.getIndicator();// 指标
		String startTime = condition.getDate().getStart(); // 开始时间
		String endTime = condition.getDate().getEnd();// 结束时间
		String visitType = condition.getVisit();// 门诊类型
		String regionCode = condition.getRegionCode();

		KpiConditionDTO kpiConditionDTO = new KpiConditionDTO();
		kpiConditionDTO.setVisitType(visitType);
		kpiConditionDTO.setKpiTimeStart(startTime);
		kpiConditionDTO.setKpiTimeEnd(endTime);
		kpiConditionDTO.setKpiCode(indication);
		kpiConditionDTO.setPeriod(Constants.perion_13); // 按照月份
		kpiConditionDTO.setSociationCode(regionCode);

		List<String> levelList = new ArrayList<String>();
		if (StringUtils.hasText(condition.getLevel())) {
			levelList.add(condition.getLevel());
		} else {
			levelList.add(Constants.type_4);
			levelList.add(Constants.type_3);
			levelList.add(Constants.type_2);
		}

		kpiConditionDTO.setGradeList(levelList);

		List<KpiGradeDTO> kpiGradeList = this.kpiCoreDataService.queryKpiByGrade(kpiConditionDTO);
		List<IndicationBeanDTO> list = null;
		if (null != kpiGradeList && kpiGradeList.size() > 0) {
			list = this.indicationHospitalHelp.getIndicationBeanDTOByHospitalLevel(kpiGradeList, kpiConditionDTO);
		}

		return list;
	}

	/**
	 * 根据级别查询医院的指标信息
	 */
	@Override
	public IndicationRegionOrgPageBean queryRegionOrgs(IndicationQueryParameterDTO condition) {

		String indication = condition.getIndicator();// 指标
		String startTime = condition.getDate().getStart(); // 开始时间
		String endTime = condition.getDate().getEnd();// 结束时间
		String visitType = condition.getVisit();// 门诊类型
		String orgLevel = condition.getOrgLevel(); // 级别
		String orgCategory = condition.getOrgCategory();
		String regionCode = condition.getRegionCode();
		int startNum = condition.getStart();
		int limit = condition.getLimit();

		KpiConditionDTO kpiConditionDTO = new KpiConditionDTO();
		kpiConditionDTO.setVisitType(visitType);
		kpiConditionDTO.setKpiTimeStart(startTime);
		kpiConditionDTO.setKpiTimeEnd(endTime);
		kpiConditionDTO.setKpiCode(indication);

		if (StringUtils.hasText(orgCategory)) {
			kpiConditionDTO.setCate(orgCategory);
		}

		if (StringUtils.hasText(orgLevel)) {
			kpiConditionDTO.setGrade(String.valueOf(orgLevel));
		}

		kpiConditionDTO.setSociationCode(regionCode);
		kpiConditionDTO.setPeriod(Constants.perion_13);

		PaginationDTO paginationDTO = new PaginationDTO();
		paginationDTO.setStart(startNum);
		paginationDTO.setLimit(limit);

		kpiConditionDTO.setPagination(paginationDTO);
		List<KpiMedicalOrgDTO> medicalOrgList = null;
		int count = this.kpiCoreDataService.queryKpiAnalyzeCountByOrg(kpiConditionDTO);

		try {
			if (count > 0) {
				medicalOrgList = this.kpiCoreDataService.queryKpiAnalyzeByOrg(kpiConditionDTO);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		IndicationRegionOrgPageBean pageBean = null;
		if (null != medicalOrgList && medicalOrgList.size() > 0) {
			pageBean = this.indicationRegionOrgHelp.processMedicalOrg(medicalOrgList, endTime);
		} else {
			pageBean = new IndicationRegionOrgPageBean();
		}
		pageBean.setTotal(count);
		// if (null != pageBean.getData() && pageBean.getData().size() > 0) {
		// Collections.sort(pageBean.getData());
		// }
		return pageBean;
	}

	/**
	 * 根据医疗机构编码查询该机构下的科室信息
	 */
	@Override
	public IndicationRegionOrgPageBean queryDepartments(IndicationQueryParameterDTO condition) {
		String indication = condition.getIndicator();// 指标
		String startTime = condition.getDate().getStart(); // 开始时间
		String endTime = condition.getDate().getEnd();// 结束时间
		String visitType = condition.getVisit();// 门诊类型
		String orgId = condition.getOrgId();
		String regionCode = condition.getRegionCode();
		int limit = condition.getLimit();
		int start = condition.getStart();

		KpiConditionDTO kpiConditionDTO = new KpiConditionDTO();
		kpiConditionDTO.setVisitType(visitType);
		kpiConditionDTO.setKpiTimeStart(startTime);
		kpiConditionDTO.setKpiTimeEnd(endTime);
		kpiConditionDTO.setKpiCode(indication);
		kpiConditionDTO.setPeriod(Constants.perion_13);
		kpiConditionDTO.setOrgCode(orgId);
		kpiConditionDTO.setSociationCode(regionCode);

		PaginationDTO pageBean = new PaginationDTO();
		pageBean.setLimit(limit);
		pageBean.setStart(start);

		kpiConditionDTO.setPagination(pageBean);

		List<KpiDeptDTO> kpiDeptList = null;

		int count = this.kpiCoreDataService.queryKpiAnalyzeCountByDept(kpiConditionDTO);

		try {
			if (count > 0) {
				kpiDeptList = this.kpiCoreDataService.queryKpiAnalyzeByDept(kpiConditionDTO);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		IndicationRegionOrgPageBean pageBeans = null;
		if (null != kpiDeptList && kpiDeptList.size() > 0) {
			pageBeans = this.indicationRegionDeptHelp.processMedicalDepartment(kpiDeptList, endTime);
		} else {
			pageBeans = new IndicationRegionOrgPageBean();
		}
		pageBeans.setTotal(count);
		if (null != pageBeans.getData() && pageBeans.getData().size() > 0) {
			Collections.sort(pageBeans.getData());
		}
		return pageBeans;
	}

	/**
	 * 根据科室编码加载对应的医生
	 */
	@Override
	public IndicationRegionOrgPageBean queryDoctors(IndicationQueryParameterDTO condition) {
		String indication = condition.getIndicator();// 指标
		String startTime = condition.getDate().getStart(); // 开始时间
		String endTime = condition.getDate().getEnd();// 结束时间
		String visitType = condition.getVisit();// 门诊类型
		String deptId = condition.getDepart(); // 科室
		String regionCode = condition.getRegionCode();
		int start = condition.getStart();
		int limit = condition.getLimit();

		KpiConditionDTO kpiConditionDTO = new KpiConditionDTO();
		kpiConditionDTO.setVisitType(visitType);
		kpiConditionDTO.setKpiTimeStart(startTime);
		kpiConditionDTO.setKpiTimeEnd(endTime);
		kpiConditionDTO.setKpiCode(indication);
		kpiConditionDTO.setDeptCode(deptId);
		kpiConditionDTO.setPeriod(Constants.perion_13);
		kpiConditionDTO.setSociationCode(regionCode);
		kpiConditionDTO.setOrgCode(condition.getOrgId());

		PaginationDTO paginationDTO = new PaginationDTO();
		paginationDTO.setStart(start);
		paginationDTO.setLimit(limit);

		kpiConditionDTO.setPagination(paginationDTO);
		List<KpiDoctorDTO> kpiDoctorList = null;
		int count = this.kpiCoreDataService.queryKpiAnalyzeCountByDoctor(kpiConditionDTO);
		try {
			if (count > 0) {
				kpiDoctorList = this.kpiCoreDataService.queryKpiAnalyzeByDoctor(kpiConditionDTO);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		IndicationRegionOrgPageBean pageBean = null;
		if (null != kpiDoctorList && kpiDoctorList.size() > 0) {
			pageBean = this.indicationDoctorHelp.processMedicalDoctor(kpiDoctorList, endTime);
		} else {
			pageBean = new IndicationRegionOrgPageBean();
		}

		pageBean.setTotal(count);
		if (null != pageBean.getData() && pageBean.getData().size() > 0) {
			Collections.sort(pageBean.getData());
		}
		return pageBean;
	}

	/**
	 * 根据医院code,去加载医院，统筹区的指标
	 */
	@Override
	public List<IndicationRegionOrgDTO> getOrgTableBasicData(IndicationQueryParameterDTO condition) {

		String indication = condition.getIndicator();// 指标
		String startTime = condition.getDate().getStart(); // 开始时间
		String endTime = condition.getDate().getEnd();// 结束时间
		String visitType = condition.getVisit();// 门诊类型
		String orgId = condition.getOrgId(); // orgCode
		String regionCode = condition.getRegionCode(); // 统筹区编码
		MedicalOrgDTO medicalOrgDto = this.iCodeLabelDAO.queryMedicalOrgDTOByOrgCode(orgId);

		if (null == medicalOrgDto) {
			return null;
		}

		String medicalCode = medicalOrgDto.getOrgCode(); // 医院编码
		medicalOrgDto.setRegionCode(regionCode);

		KpiConditionDTO kpiConditionDTO = new KpiConditionDTO();
		kpiConditionDTO.setVisitType(visitType);
		kpiConditionDTO.setKpiTimeStart(startTime);
		kpiConditionDTO.setKpiTimeEnd(endTime);
		kpiConditionDTO.setKpiCode(indication);
		kpiConditionDTO.setPeriod(Constants.perion_13);

		List<IndicationRegionOrgDTO> list = new ArrayList<IndicationRegionOrgDTO>();

		if (StringUtils.hasText(regionCode)) {
			kpiConditionDTO.setSociationCode(regionCode);
			List<KpiSociationDTO> sociationList = this.kpiCoreDataService.queryKpiBySoc(kpiConditionDTO);

			if (null != sociationList && sociationList.size() > 0) {
				IndicationRegionOrgDTO sociationRegion = this.indicationRegionOrgHelp.processSocial(sociationList,
						medicalOrgDto, endTime);

				list.add(sociationRegion);

			}
		}

		if (StringUtils.hasText(medicalCode)) {
			kpiConditionDTO.setOrgCode(medicalCode);
			List<KpiMedicalOrgDTO> medicalOrgList = this.kpiCoreDataService.queryKpiByOrg(kpiConditionDTO);
			if (null != medicalOrgList && medicalOrgList.size() > 0) {
				IndicationRegionOrgDTO medicalRegionBean = this.indicationRegionOrgHelp.processIndicationMedicalOrg(
						medicalOrgList, medicalOrgDto, endTime);

				list.add(medicalRegionBean);
				Collections.sort(list);
			}
		}

		return list;
	}

	/**
	 * 处理根据科室id,去加载科室，医院，统筹区的指标
	 */
	@Override
	public List<IndicationRegionOrgDTO> getDeptTableBasicData(IndicationQueryParameterDTO condition) {

		String indication = condition.getIndicator();// 指标
		String startTime = condition.getDate().getStart(); // 开始时间
		String endTime = condition.getDate().getEnd();// 结束时间
		String visitType = condition.getVisit();// 门诊类型
		String deptId = condition.getDepart(); // 科室的编码
		String regionCode = condition.getRegionCode(); // 统筹区编码

		String orgId = condition.getOrgId();
		//

		MedicalOrgDTO medicalOrgDto = this.iCodeLabelDAO.queryMedicalOrgDTOByOrgCode(orgId);

		medicalOrgDto.setRegionCode(regionCode);

		KpiConditionDTO kpiConditionDTO = new KpiConditionDTO();
		kpiConditionDTO.setVisitType(visitType);
		kpiConditionDTO.setKpiTimeStart(startTime);
		kpiConditionDTO.setKpiTimeEnd(endTime);
		kpiConditionDTO.setKpiCode(indication);
		kpiConditionDTO.setPeriod(Constants.perion_13);

		List<IndicationRegionOrgDTO> list = new ArrayList<IndicationRegionOrgDTO>();

		// 处理统筹区
		if (StringUtils.hasText(regionCode)) {
			kpiConditionDTO.setSociationCode(regionCode);
			List<KpiSociationDTO> sociationList = this.kpiCoreDataService.queryKpiBySoc(kpiConditionDTO);
			if (null != sociationList && sociationList.size() > 0) {
				IndicationRegionOrgDTO sociationRegion = this.indicationRegionOrgHelp.processSocial(sociationList,
						medicalOrgDto, endTime);

				list.add(sociationRegion);

			}
		}

		// 处理医院
		if (StringUtils.hasText(orgId)) {
			kpiConditionDTO.setOrgCode(orgId);
			List<KpiMedicalOrgDTO> medicalOrgList = this.kpiCoreDataService.queryKpiByOrg(kpiConditionDTO);
			if (null != medicalOrgList && medicalOrgList.size() > 0) {
				IndicationRegionOrgDTO medicalRegionBean = this.indicationRegionOrgHelp.processIndicationMedicalOrg(
						medicalOrgList, medicalOrgDto, endTime);

				list.add(medicalRegionBean);
				Collections.sort(list);
			}

		}

		// 处理科室
		if (StringUtils.hasText(orgId)) {
			kpiConditionDTO.setDeptCode(deptId);
			List<KpiDeptDTO> kpiDeptList = this.kpiCoreDataService.queryKpiByDept(kpiConditionDTO);
			if (null != kpiDeptList && kpiDeptList.size() > 0) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("deptId", deptId);
				map.put("orgCode", orgId);
				IndicationDeptDTO deptDTO = this.iCodeLabelDAO.queryIndicationDepartDTOByDeptCode(map);
				if (deptDTO != null) {
					medicalOrgDto.setDeptName(deptDTO.getMedicalDeptName());
				}
				medicalOrgDto.setDeptCode(deptId);// 科室id
				IndicationRegionOrgDTO medicalRegionBean = this.indicationRegionOrgHelp.processIndicationDeptment(
						kpiDeptList, medicalOrgDto, endTime);
				list.add(medicalRegionBean);
				Collections.sort(list);
			}
		}

		return list;
	}

	@Override
	public IndicationTitleDTO queryIndicationTitleDTOByKpiId(String kpiId) {
		IndicatorBaseDTO bean = this.iCodeLabelDAO.queryIndicatorBaseDTOByKpiId(kpiId);

		if (bean != null) {
			IndicationTitleDTO titleBean = new IndicationTitleDTO();
			titleBean.setLabel(bean.getKpiName());
			titleBean.setName(bean.getKpiId());
			titleBean.setType(Integer.parseInt(bean.getVisitType()));
			return titleBean;
		}

		return null;
	}
}
